package com.dacool.view;

import com.dacool.app.sale.R;
import com.dacool.util.ViewUtils;

import android.content.Context;
import android.graphics.Color;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TabItem {
	public String tabName;
	public TabItemCustomView customView;

	public void setBadgeValue(String value) {
		customView.badgeView.setText(value);
		customView.badgeView
				.setVisibility((value == null || value.length() == 0) ? View.GONE
						: View.VISIBLE);
		if (Looper.myLooper() != Looper.getMainLooper())
			customView.postInvalidate();
	}

	public static class TabItemCustomView extends RelativeLayout {
		private int idnormal;
		private int idselected;
		private ImageView iv;
		private TextView badgeView;

		public TabItemCustomView(Context context, AttributeSet attrs) {
			super(context, attrs);
			iv = new ImageView(context);
			iv.setScaleType(ScaleType.FIT_CENTER);
			iv.setImageResource(idnormal);
			RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
			this.addView(iv, rlp);
			this.setBackgroundColor(Color.TRANSPARENT);

			View anchor = new View(context);
			rlp = new RelativeLayout.LayoutParams(1, 1);
			rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
			anchor.setVisibility(View.INVISIBLE);
			anchor.setId(10086);
			this.addView(anchor, rlp);

			badgeView = new TextView(getContext());
			badgeView.setBackgroundResource(R.drawable.badge_background);
			badgeView.setTextColor(Color.WHITE);
			badgeView.setGravity(Gravity.CENTER);
			rlp = new RelativeLayout.LayoutParams(ViewUtils.rp(20),
					ViewUtils.rp(20));
			badgeView.setTextSize(10);
			rlp.addRule(RelativeLayout.ABOVE, anchor.getId());
			rlp.addRule(RelativeLayout.RIGHT_OF, anchor.getId());
			badgeView.setVisibility(View.GONE);
			this.addView(badgeView, rlp);

		}

		public void setImage(int resId) {
			idnormal = resId;
		}

		public void setSelectedImage(int resId) {
			idselected = resId;
		}

		@Override
		public void setSelected(boolean selected) {
			// TODO Auto-generated method stub
			super.setSelected(selected);
			if (selected && idselected != 0) {
				iv.setImageResource(idselected);
			} else {
				iv.setImageResource(idnormal);
			}
		}
	}
}
